﻿using System;

public class LoopInfo:BaseInfo
{
    /// <summary>
    /// 循环次数
    /// </summary>
    private int _loopCount;

    /// <summary>
    /// 当前循环次数
    /// </summary>
    private int _curLoopCount;

    /// <summary>
    /// Loop回调
    /// </summary>
    private Action _loopAction;

    /// <summary>
    /// 结束时间
    /// </summary>
    private float _endTime;

    /// <summary>
    /// 间隔
    /// </summary>
    private float _interval;

    /// <summary>
    /// 当前时间
    /// </summary>
    private float _curTime;

    /// <summary>
    /// 是否可以被移除
    /// </summary>
    private bool _canRemove;

    public LoopInfo (int loopCount,Action loopAction,int loopInterval)
    {
        _loopCount = loopCount;
        _loopAction = loopAction;
        _interval = loopInterval;
    }

    public override bool CanRemove => _canRemove;

    public override bool CanInvoke()
    {
        return _curTime > _endTime;
    }

    public override void Invoke()
    {
        _loopAction?.Invoke();
        ResetTimer();
        if (_loopCount <= 0)
            return;

        _curLoopCount++;
        if(_curLoopCount == _loopCount)
        {
            _canRemove = true;
        }
    }

    public override void UpdateTime(float time)
    {
        _curTime = time;
    }

    /// <summary>
    /// 重置下一个计时器
    /// </summary>
    private void ResetTimer()
    {
        _endTime = _curTime + _interval;
    }
}